﻿using CefDemo.cef;
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CefDemo
{
    public partial class Form1 : Form
    {

        private ChromiumWebBrowser browser;
        private ChromiumWebBrowser browserStatistic;
        private ChromiumWebBrowser browserMenu;

        public Form1()
        {
            InitializeComponent();

            browser = BrowserFactory.InitWebViewWithEmbeddedResource("html/index.html");
            tabFirst.Controls.Add(browser);
            browser.IsBrowserInitializedChanged += browserChanged;

            browser.BindObject("main", new CustomerBindObject(this), isAsync: false);

            initStatistic();

            initMenu();
        }

        private void initStatistic()
        {
            browserStatistic = BrowserFactory.InitWebViewWithUrl("about:blank");
            tabSecond.Controls.Add(browserStatistic);
        }

        private void initMenu()
        {
            browserMenu = BrowserFactory.InitWebViewWithEmbeddedResource("html/menu.html");
            panelMenu.Controls.Add(browserMenu);
        }

        private void browserChanged(object sender, EventArgs e)
        {
            //Console.WriteLine("ttttttttttttt");
            if (browser.IsBrowserInitialized)
            {
                browser.ShowDevTools();
            }
        }

        private void FrameLoadFunc(object sender, FrameLoadEndEventArgs e)
        {
            browser.ShowDevTools();
        }


        class CustomerBindObject : AbstractBindObject<Form1>
        {
            public CustomerBindObject(Form1 form) : base(form)
            {
            }

            public void show()
            {
                RunOnUI(() =>
                {
                    UIHelper.OpenImageSelectDialog(item =>
                    {
                        MessageBox.Show(item);
                    });
                });
            }

            public void showName(string name)
            {
                MessageBox.Show("js传过来的name:" + name);
            }

            public string getName()
            {
                return "从C#中返回的字符串，同步";
            }

            public void getNameAsync(IJavascriptCallback callback)
            {
                RunOnUI(async () =>
                {
                    await Task.Delay(2000);
                    //callback.Invoke("从C#中回调的字符串");
                    await callback.ExecuteAsync("tttttttt");
                });
            }

            public void chooseImage(IJavascriptCallback callback)
            {
                RunOnUI(() => UIHelper.OpenImageSelectDialog(async item => await callback.ExecuteAsync(item)));
            }

            public void showNameAndAge(string name, int age)
            {
                MessageBox.Show(string.Format("js传过来的name: {0},age: {1}", name, age));
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            browserStatistic.Load("https://www.iconfont.cn/search/index?searchType=icon&q=%E8%AE%BE%E7%BD%AE&page=2");
        }

        private async void button2_ClickAsync(object sender, EventArgs e)
        {
            //var stu = new Student()
            //{
            //    name = "zhangsan",
            //    age = 12
            //};
            //browser.ExecuteJsAsync("show", stu);
            var obj = await browser.EvaluateJsAsync<string>("getJsObject", null);
            MessageBox.Show(obj);
        }
    }

    class Student
    {
        public string name { set; get; }
        public int age { set; get; }
    }
}
